// house_pkg/pages/locate/index.ts
import qqmap from '../../../utils/qqmap'
Page({
  data: {
    points: [],
    address: ''
  },
  onLoad () {
    this.getUserInfo()
  },
  //获取经纬度
  async getUserInfo () {
    const { latitude, longitude } = await wx.getLocation()
    // console.log(latitude, longitude)
    this.getPoint(latitude, longitude)
    this.reverseGeocoder(latitude, longitude)
  },
  //根据经纬度获取附近小区
  getPoint (latitude, longitude) {
    qqmap.search({
      keyword: '住宅小区', //搜索关键词
      location: { latitude, longitude },
      page_size: 5,
      success: ({ data }) => {
        // console.log(data)
        const points = data.map(({ id, title, _distance }) => ({ id, title, _distance }))
        // console.log(points)
        this.setData({ points })
      }
    })
  },

  //逆解析地址
  reverseGeocoder (latitude, longitude) {
    qqmap.reverseGeocoder({
      location: { latitude, longitude },
      success: res => {
        // console.log(res.result.address)
        this.setData({ address: res.result.address })
      }
    })
  },

  //重新获取地址
  async chooseLocation () {
    const { latitude, longitude, name } = await wx.chooseLocation()
    this.getPoint(latitude, longitude)
    this.setData({ address: name })
  }
})
